package cn.wolfcode.rabc.controller;

import cn.wolfcode.rabc.domain.Employee;
import cn.wolfcode.rabc.qo.JsonResult;
import cn.wolfcode.rabc.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(String username, String password, HttpSession session) {
        Employee employee = employeeService.login(username, password);
        if (employee == null) {
            return new JsonResult("用户名或密码错误", false);
        }
        // 登录成功
        session.setAttribute("EMPLOYEE_IN_SESSION", employee);
        return new JsonResult("登录成功", true);
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("EMPLOYEE_IN_SESSION");
        return "redirect:/static/login.html";
    }
}
